import java.security._;
import java.math._;

object Utils {
  def byteToHex(buf: Array[Byte]): String = buf.map("%02X" format _).mkString
  
  def hash(input : String) : Array[Byte] = {
    val mDigest = MessageDigest.getInstance("MD5");
    mDigest.digest(input.getBytes());
  }
  
  def getKeyDigitValue(key : String, l : Integer) : Integer = {
    val c = key.charAt(l).toString
    java.lang.Integer.parseInt(c, 16)
  }
  
  def getNodeDiff(A : String, B : String) : BigInteger = {
    val a = new BigInteger(A, 16)
    val b = new BigInteger(B, 16)
    a.subtract(b).abs 
  }
  
  def shl(A : String, B : String) : Int = {
    val length = Math.min(A.length(), B.length())
    var prefixSize = 0
    var continue = true
    while(continue){
      if (A.charAt(prefixSize) != B.charAt(prefixSize)){
        continue = false
      }
      else {
        prefixSize += 1
      }
      
      if (prefixSize == length){
        continue = false
      }
    }
    
    prefixSize
  }
  
  def calculateAVG(list : List[Double]) : Double = {
    var sum = 0.0
    list.foreach(x => sum += x)
    sum / list.length
  }
  
}